﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.Collections.ObjectModel;
using System.Xml.Linq;
using ProtoType.DataStorage;
using ProtoType.ProtoTypeService;
using ProtoType;

namespace ProtoType
{
    public partial class ManageControlList : PhoneApplicationPage
    {
        ObservableCollection<ControlDesign> controlList = null;
        string selectedfid = "";
        ProtoTypeServiceClient client = new ProtoTypeServiceClient();
        DataStructureStore dss = new DataStructureStore();
        AppSettings apps = new AppSettings();
        bool NewPageInstance = false;
        public ManageControlList()
        {
            InitializeComponent();
            NewPageInstance = true;
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (NavigationContext.QueryString.TryGetValue("fid", out selectedfid))
            {
                if (NewPageInstance)
                {
                    LoadFormControls(selectedfid);
                }
                NewPageInstance = false;

            }
        }

        void LoadFormControls(string formid)
        {
            if (apps.IsolatedStorageSavingSetting)
            {
                LoadFromIsoStore();
            }
            if (apps.WebServiceSavingSetting)
            {
                client.LoadformControlListAsync(formid);
                client.LoadformControlListCompleted += new EventHandler<LoadformControlListCompletedEventArgs>(client_LoadformControlListCompleted);
            }
        }

        private void LoadFromIsoStore()
        {
            LoadControlList(dss.LoadformControlList(selectedfid));
        }

        void client_LoadformControlListCompleted(object sender, LoadformControlListCompletedEventArgs e)
        {
            LoadControlList(e.Result.ToObservableCollection()); 
        }
        void LoadControlList(ObservableCollection<XElement> formControllist)
        {
            controlList = new ObservableCollection<ControlDesign>();
            if (formControllist.ToList().Count != 0)
            {
                foreach (XElement fc in formControllist.Elements("control"))
                {
                    ControlDesign cd = new ControlDesign();
                    cd.Id = int.Parse(fc.Attribute("id").Value);
                    cd.CName = fc.Element("cname").Value;
                    cd.CType = fc.Element("ctype").Value;
                    controlList.Add(cd);
                }
            }
            lstControls.ItemsSource = controlList;
        
        }

        private void lstControls_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (lstControls.SelectedIndex != -1)
            {
                //ControlDesign cd = new ControlDesign();
                //cd = lstControls.SelectedItem as ControlDesign;
                //NavigationService.Navigate(new Uri("/EditControl.xaml?fid=" + selectedfid + "&cid=" + cd.Id.ToString(), UriKind.Relative));
            }
        }
        private void newButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/AddControl.xaml?fid=" + selectedfid, UriKind.Relative));
        }

    }
}